﻿using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EHTWebsite.Code;
using EHTWebsiteProject.Code.Entity;
namespace EHTWebsiteProject.Administrator
{
    public partial class AdminCustomersCategorys : System.Web.UI.Page
    {
        private Int32 NhomDanhMucID = -1;
        private Int32 pageIndex = 1;
        private Int32 pageSize = 10;
        private Int32 totalRecord = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                pageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
            }
            catch(Exception ex)
            {
                pageIndex = 1;
            }
            try
            {
                NhomDanhMucID = Int32.Parse(Request.QueryString["NhomDanhMucID"].ToString());
            }
            catch(Exception ex)
            {
                NhomDanhMucID = -1;
            }
            pageSize = this.PaggingControl1.PageSize;
            if (!IsPostBack)
            {
               // this.ddlNhomDanhMuc.SelectedValue = DanhMucID.ToString();
                LoadDataDanhMucKhachHangs(NhomDanhMucID);
            }
            
        }
        private void LoadDataDanhMucKhachHangs(Int32 NhomDanhMucID)
        {
            DanhMucKhachhang theDanhMuc = new DanhMucKhachhang();
            theDanhMuc.DanhMucKhachHangID = HangSo.DEFAULT_ID;
            theDanhMuc.TenDanhMucKhachHangVN = "";
            theDanhMuc.TenDanhMucKhachHangOther = "";
            theDanhMuc.NhomDanhMucID = NhomDanhMucID;
            DataSet dsDanhMucKhachHang=DanhMucKhachHangProvider.GetDanhMucKhachHangPaggin(theDanhMuc,pageIndex,pageSize,out totalRecord);
            if (dsDanhMucKhachHang.Tables[0].Rows.Count>0)
            {
                rpDanhMucKhachHang.DataSource = dsDanhMucKhachHang;
                rpDanhMucKhachHang.DataBind();
                foreach (RepeaterItem item in rpDanhMucKhachHang.Items)
                {
                    CheckBox ckPublish = (CheckBox)item.FindControl("ckPublish");
                    if (ckPublish.Checked == true)
                    {
                        ((ImageButton)item.FindControl("btnPublish")).ImageUrl = "Templates/Images/btncheck.png";
                    }
                    else
                    {
                        ((ImageButton)item.FindControl("btnPublish")).ImageUrl = "Templates/Images/unpublish.jpg";
                    }
                }
            }
            this.PaggingControl1.PageIndex = pageIndex;
            this.PaggingControl1.TotalRecord = totalRecord;
            this.PaggingControl1.PageURL = "AdminCustomersCategorys.aspx";
        }

        protected void ddlNhomDanhMuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDataDanhMucKhachHangs(-1);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminCustomerCategoryIU.aspx");
        }

        protected void rpDanhMucKhachHang_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string cmdName = e.CommandName;
            Int32 DanhMucKhachHangId = -1;
            try
            {
                DanhMucKhachHangId = Int32.Parse(e.CommandArgument.ToString());
            }
            catch(Exception ex)
            {}
            if (cmdName == "delete1")
            {
                DanhMucKhachHangProvider.DeleteDanhMucKhachHang(DanhMucKhachHangId);
                LoadDataDanhMucKhachHangs(-1);
               // Response.Redirect("AdminCustomersCategorys.aspx");
            }
            else if (cmdName == "publish")
            {
                DanhMucKhachHangProvider.ChangePublishByDanhMucKhachHangID(DanhMucKhachHangId);
                //LoadDataDanhMucKhachHangs(Int32.Parse(ddlNhomDanhMuc.SelectedValue));
                Response.Redirect("AdminCustomersCategorys.aspx");
            }
            else if (cmdName == "change_number_of_order")
            {
                try
                {
                    Int32 numberOfOrder = Int32.Parse(((TextBox)e.Item.FindControl("txtNumberOfOrder")).Text.Trim());
                    DanhMucKhachHangProvider.UpdateNumberOfOrderByDanhMucKhachHangId(DanhMucKhachHangId,numberOfOrder);
                }
                catch(Exception ex)
                {
                }
               // LoadDataDanhMucKhachHangs(Int32.Parse(ddlNhomDanhMuc.SelectedValue));
                Response.Redirect("AdminCustomersCategorys.aspx");
            }
            else if(cmdName=="save_all")
            {
                 foreach (RepeaterItem item in rpDanhMucKhachHang.Items)
                {
               
                    Int32 numberOfOrder = Int32.Parse(((TextBox)item.FindControl("txtNumberOfOrder")).Text.Trim());
                    DanhMucKhachHangId = Int32.Parse(((Literal)item.FindControl("ltrDanhMucKhachHangID")).Text.Trim());
                    DanhMucKhachHangProvider.UpdateNumberOfOrderByDanhMucKhachHangId(DanhMucKhachHangId,numberOfOrder);
                }
                 //LoadDataDanhMucKhachHangs(Int32.Parse(ddlNhomDanhMuc.SelectedValue));
                 Response.Redirect("AdminCustomersCategorys.aspx");
            }
        }
    }
}